'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, DECIMAL, TEXT } = app.Sequelize;
  const Order = app.model.define(
      'order',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            order_no: STRING(255),
            member_id: INTEGER(11),
            member_name: STRING(255),
            order_status: INTEGER(11),
            status: INTEGER(11),
            back_reason: STRING(255),
              address_id: INTEGER(11),
              address: STRING(255),
            address_nickname: STRING(255),
            address_phone: STRING(255),
            pay_way: INTEGER(11),
            amount: DECIMAL(10,2),
            count: INTEGER(11),
            send_time: STRING(255),
          image: STRING(255),
          result: STRING(255),
          staff_id: INTEGER(11),
          staff_remark: STRING(255),
            files: TEXT,
          confirm_time: {
              type: app.Sequelize.DATE,
              allowNull: true,
              get() {
                  return dayjs(this.getDataValue('confirm_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          accept_time: {
              type: app.Sequelize.DATE,
              allowNull: true,
              get() {
                  return dayjs(this.getDataValue('accept_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          create_time: {
              type: app.Sequelize.DATE,
              allowNull: false,
              get() {
                  return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          updated_time: {
              type: app.Sequelize.DATE,
              allowNull: false,
              get() {
                  return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          }
      },
      {
          tableName: 'order',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
    Order.associate = function () {
        Order.hasMany(app.model.OrderDetail, { foreignKey: 'order_id', targetKey: 'id'})
        Order.belongsTo(app.model.Member, { foreignKey: 'member_id', targetKey: 'id'})
        Order.belongsTo(app.model.Staff, { foreignKey: 'staff_id', targetKey: 'id'})
    }
    return Order
};
